package com.wm.blog_admin.event;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wm.blog_admin.dao.TMenuDAO;
import com.wm.blog_common.domain.TIconDO;
import com.wm.blog_common.entity.TMenu;
import com.wm.blog_common.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @author wm_yu  监听图标事件变化
 * @date 2020-4-29 11:23
 */
@Component
@Slf4j
public class IconEventListener implements ApplicationListener<IconEvent> {

    @Autowired
    private TMenuDAO tMenuDAO;

    @Override
    public void onApplicationEvent(IconEvent event) {
        TIconDO icon = event.getIcon();
        //修改menu中的圖標数据
        //校验数据是否全为null,boolean类型的默认值
        boolean b = CommonUtil.checkNullAndEmpty(icon);
        if(b){
           log.error("监听icon对象为null,不处理流程..");
           return;
        }
        if(StringUtils.isEmpty(icon.getOldType()) || StringUtils.isEmpty(icon.getType())){
            return;
        }
      //更新菜单中的图标数据
        TMenu menu = new TMenu();
        menu.setType(icon.getType());
        menu.setSize(icon.getSize());
        menu.setColor(icon.getColor());
        UpdateWrapper<TMenu> wrapper = new UpdateWrapper<>();
        wrapper.eq("type",icon.getOldType());
        synchronized (TMenuDAO.class){
            boolean update = tMenuDAO.update(menu,wrapper);
            log.info("图标监听更新是否成功:[{}]",update);
        }
    }
}
